package com.devframe.web.taglib;

import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import com.devframe.sysbase.model.TSysMenu;
import com.devframe.sysconfig.service.SysRightService;
import com.devframe.web.filter.ActionFilter;
import com.devframe.web.loader.MyWebContextLoader;


/**
 * 
 * <pre>
 * &lt;b&gt;&lt;font color=&quot;blue&quot;&gt;SelectTag&lt;/font&gt;&lt;/b&gt;
 * </pre>
 * 
 * <pre>
 * &lt;b&gt; --页面路径信息输出--&lt;/b&gt;
 * </pre>
 * </pre>
 * <pre>
 * 
 * &lt;b&gt;--样例--&lt;/b&gt; &gt;d:dateTime value="${log.logDate}"/>
 * 
 * </pre>
 * 
 * JDK版本：JDK1.4
 * 
 * 
 */
public class PagePathTag extends AbstractBaseTag {
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 932991928216241306L;

    /**
     * split 分隔符
     */
    private String split = "&gt;&gt;";

    /**
     * split2 功能和菜单之间的分隔符
     */
    private String split2 = "——";

    /**
     * defaultValue 默认值
     */
    private String defaultValue;
    
    /**
     * menuCode 功能菜单代码，用于指定代码显示路径
     */
    private String menuCode;

    // ////////////////////////////////////////////
    /**
     * 标签处理逻辑，根据代码显示名称
     * 
     * @return SKIP_BODY
     */
    public int doStartTag() {
        return SKIP_BODY;
    }

    /**
     * doEndTag
     * 
     * @return EVAL_PAGE
     * @throws JspException
     *             JspException
     */
    public int doEndTag() throws JspException {
        try {
            SysRightService sysRightService = (SysRightService) MyWebContextLoader.getInstance().getBean("sysRightService");
            StringBuffer result = new StringBuffer();
            Map menuInfo = null;
            if(menuCode!=null){
                menuInfo = sysRightService.getSysRightInfo(menuCode);
            } else {
                menuInfo = (Map) pageContext.getRequest().getAttribute(ActionFilter.MENU_INFO_KEY);
            }
            if (menuInfo != null) {
                String menuCode = (String) menuInfo.get("FUNC_CODE");
                String menuType = null;
                Object obj= menuInfo.get("MENU_TYPE");
                if(obj!=null){
                	menuType=obj.toString();
                }

                TSysMenu menu = null;
                Map rightInfo = sysRightService.getSysRightInfo(menuCode);
                while(rightInfo!=null){
                    result.insert(0,rightInfo.get("MENU_NAME"));
                    String parentCode= (String) rightInfo.get("PARENT_CODE");
                    if(menuCode.equals(parentCode)){
                        break;
                    }
                    if(parentCode!=null && !"".equals(parentCode)){
                        result.insert(0,this.split);
                        rightInfo=sysRightService.getSysRightInfo(parentCode);
                    } else {
                        rightInfo=null;
                    }
                }
            }
            if(menuCode!=null){
                if (result.length() == 0 && defaultValue != null) {
                    result.append(defaultValue);
                } else if (result.length() == 0) {
                    result.append(menuCode);
                }                
            } else {
                if (result.length() == 0 && defaultValue != null) {
                    result.append(defaultValue);
                } else if (result.length() == 0) {
                    result.append("<span id=\"d_location\"></span><script>$(function(){$('#d_location').html(window.document.title);});</script>");
                }
            }
            JspWriter writer = null;
            try {
                writer = this.getPreviousOut();
            } catch (Exception e1) {
                writer = pageContext.getOut();
            }
            try {
                writer.print(result);
                result = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return EVAL_PAGE;
        } catch (Exception e) {
            e.printStackTrace();
            return EVAL_PAGE;
        }
    }

    /**
     * release
     * 
     * @see javax.servlet.jsp.tagext.BodyTagSupport#release()
     */
    public void release() {
        super.release();
    }

    public String getSplit() {
        return split;
    }

    public void setSplit(String split) {
        this.split = split;
    }

    public String getDefaultValue() {
        return defaultValue;
    }

    public void setDefaultValue(String defaultValue) {
        this.defaultValue = defaultValue;
    }

    public String getSplit2() {
        return split2;
    }

    public void setSplit2(String split2) {
        this.split2 = split2;
    }

    public String getMenuCode() {
        return menuCode;
    }

    public void setMenuCode(String menuCode) {
        this.menuCode = menuCode;
    }

}
